package com.tanhua.manage.service;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.common.mapper.UserInfoMapper;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.dubbo.server.vo.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;


    public UserInfo queryUserInfoByUserId(Long userId) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        return this.userInfoMapper.selectOne(queryWrapper);
    }

    public List<UserInfo> queryUserInfoList(QueryWrapper<UserInfo> queryWrapper) {
        return this.userInfoMapper.selectList(queryWrapper);
    }

    /**
     * 根据用户id列表查询用户信息
     *
     * @param userIdList
     * @return
     */
    public List<UserInfo> queryUserInfoByUserIdList(Collection<?> userIdList) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("user_id", userIdList);
        return this.queryUserInfoList(queryWrapper);
    }


//    public List<Map<String, Object>> countIndustryDistribution(String sd, String ed) {
//        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
//        queryWrapper.select("COUNT(sex)");
//        queryWrapper.ge("created", sd);
//        queryWrapper.le("created", ed);
//        queryWrapper.groupBy("sex");
//        List<Map<String, Object>> maps = userInfoMapper.selectMaps(queryWrapper);
//        return maps;
//    }

    public List<Map<String, Object>> countGenderDistribution(Date sd, Date ed) {

        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("COUNT(sex) AS count ,sex");
        queryWrapper.ge("created", sd);
        queryWrapper.le("created", ed);
        queryWrapper.groupBy("sex");
        return userInfoMapper.selectMaps(queryWrapper);
    }

    public List<Map<String, Object>> countIndustryDistribution(Date sd, Date ed) {

        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("COUNT(industry) AS count ,industry");
        queryWrapper.ge("created", sd);
        queryWrapper.le("created", ed);
        queryWrapper.groupBy("industry");
        queryWrapper.orderByDesc("count");
        queryWrapper.last("limit 10");
        return userInfoMapper.selectMaps(queryWrapper);
    }

    public List<Map<String, Object>> countAgeDistribution(Date sd, Date ed) {

        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("case \n" +
                "    when age <= 17 then '0-17' \n" +
                "    when age <= 23 then '18-23' \n" +
                "    when age <= 30 then '24-30' \n" +
                "    when age <= 40 then '31-40' \n" +
                "    when age <= 50 then '41-50'  \n" +
                "    else '50+'  \n" +
                "\t\tend as AgeGroup ,COUNT(*)as count ");
        queryWrapper.ge("created", sd);
        queryWrapper.le("created", ed);
        queryWrapper.groupBy("AgeGroup");
        return userInfoMapper.selectMaps(queryWrapper);
    }

    public List<Map<String, Object>> countLocalDistribution(Date sd, Date ed) {

        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.select(
                "    case \n" +
                        "    when city\tLIKE '北京市%' then '北京市' \n" +
                        "    when city\tLIKE '河北省%' then '河北省' \n" +
                        "    when city\tLIKE '山西省%' then '山西省' \n" +
                        "    when city\tLIKE '辽宁省%' then '辽宁省' \n" +
                        "    when city\tLIKE '吉林省%' then '吉林省' \n" +
                        "    when city\tLIKE '黑龙江省%' then '黑龙江省' \n" +
                        "    when city\tLIKE '江苏省%' then '江苏省' \n" +
                        "    when city\tLIKE '浙江省%' then '浙江省' \n" +
                        "    when city\tLIKE '安徽省%' then '安徽省' \n" +
                        "    when city\tLIKE '福建省%' then '福建省' \n" +
                        "    when city\tLIKE '江西省%' then '江西省' \n" +
                        "    when city\tLIKE '山东省%' then '山东省' \n" +
                        "    when city\tLIKE '河南省%' then '河南省' \n" +
                        "    when city\tLIKE '湖北省%' then '湖北省' \n" +
                        "    when city\tLIKE '湖南省%' then '湖南省' \n" +
                        "    when city\tLIKE '广东省%' then '广东省' \n" +
                        "    when city\tLIKE '海南省%' then '海南省' \n" +
                        "    when city\tLIKE '四川省%' then '四川省' \n" +
                        "    when city\tLIKE '贵州省%' then '贵州省' \n" +
                        "    when city\tLIKE '云南省%' then '云南省' \n" +
                        "    when city\tLIKE '陕西省%' then '陕西省' \n" +
                        "    when city\tLIKE '甘肃省%' then '甘肃省' \n" +
                        "    when city\tLIKE '青海省%' then '青海省' \n" +
                        "    when city\tLIKE '台湾省%' then '台湾省' \n" +
                        "    when city\tLIKE '内蒙古%' then '内蒙古' \n" +
                        "    when city\tLIKE '广西%' then '广西' \n" +
                        "    when city\tLIKE '西藏%' then '西藏' \n" +
                        "    when city\tLIKE '宁夏%' then '宁夏' \n" +
                        "    when city\tLIKE '新疆%' then '新疆' \n" +
                        "    when city\tLIKE '天津市%' then '天津市' \n" +
                        "    when city\tLIKE '上海市%' then '上海市' \n" +
                        "    when city\tLIKE '重庆市%' then '重庆市' \n" +
                        "    when city\tLIKE '香港%' then '香港' \n" +
                        "    when city\tLIKE '澳门%' then '澳门' \n" +
                        "\t\telse '未知'  \n" +
                        "\t\tend as City ,COUNT(*)as count ");
        queryWrapper.ge("created", sd);
        queryWrapper.le("created", ed);
        queryWrapper.groupBy("City");
        return userInfoMapper.selectMaps(queryWrapper);
    }

    public  IPage<UserInfo> getUsersList(Integer page, Integer pageSize, Integer id, String nickname, String city) {
        Page<UserInfo> page1 = new Page<>(page, pageSize);
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtil.isEmpty(id)) {
            queryWrapper.like("user_id", id);
        }
        if (!ObjectUtil.isEmpty(id)) {
            queryWrapper.like("nick_name", nickname);
        }
        if (!ObjectUtil.isEmpty(id)) {
            queryWrapper.like("city", city);
        }
        queryWrapper.orderByAsc("id");
        return userInfoMapper.selectPage(page1, queryWrapper);
    }
}
